<template>
  <div class="app-container home">
    <el-tabs v-model="tabName" type="card" @tab-click="handleTabChange">
      <el-tab-pane name="cooperate" label="协同任务" />
      <!-- <el-tab-pane name="audit" label="审核任务" /> -->
    </el-tabs>
    <div class="fr">
      <el-form :inline="true" :model="formInline" class="">
        <el-form-item label="文件名称">
          <el-input
            v-model="formInline.docName"
            clearable
            placeholder="请输入"
            size="small"
          ></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-select
            v-model="formInline.status"
            clearable
            placeholder="请选择"
            size="small"
          >
            <el-option
              :label="item.label"
              :value="item.value"
              :key="item.label"
              v-for="item of options"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch" size="small"
            >搜索</el-button
          >
          <el-button type="primary" plain @click="handleReset" size="small"
            >重置</el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <el-table v-loading="loading" :data="tableData">
      <el-table-column
        label="序号"
        type="index"
        width="50"
        :index="indexMethod"
      >
      </el-table-column>
      <el-table-column prop="docName" label="文件名称"> </el-table-column>
      <el-table-column prop="assigner" label="负责人"
        ><template slot-scope="scope">
          <template v-if="tabName === 'cooperate'"
            >{{ scope.row.assignerName }} / {{ scope.row.assigner }}</template
          >
          <template v-else
            >{{ scope.row.initiatorName }} /
            {{ scope.row.initiatorNo }}</template
          >
        </template></el-table-column
      >
      <el-table-column prop="createTime" label="任务发起时间" />
      <el-table-column
        v-if="tabName === 'cooperate'"
        prop="deadlineTime"
        label="截止时间"
      />
      <el-table-column prop="statusName" label="状态" />
      <el-table-column prop="index" label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleEditContext(scope.row)"
            >去处理</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      class="p20 fr"
      v-if="total > 0"
      :background="true"
      :current-page.sync="queryParams.pageNum"
      :page-size.sync="queryParams.pageSize"
      :layout="'total, sizes, prev, pager, next, jumper'"
      :page-sizes="[10, 30, 50, 100]"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handlepageNumChange"
    />
  </div>
</template>

<script>
import qs from "qs";
import { getCooperateList, getAuditList } from "@/api/business";
export default {
  name: "IndexTodo",
  data() {
    return {
      tabName: "cooperate", // audit | cooperate
      formInline: {
        docName: "",
        status: "",
      },
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      loading: false,
      total: 0,
      tableData: [],
    };
  },
  computed: {
    options() {
      const auditOptions = [
        {
          value: "1",
          label: "待审核",
        },
        {
          value: "2",
          label: "审核中",
        },
        {
          value: "3",
          label: "审核通过",
        },
        {
          value: "4",
          label: "审核驳回",
        },
      ];
      const cooperateOptions = [
        {
          value: "1",
          label: "未开始",
        },
        {
          value: "2",
          label: "协作中",
        },
        // {
        //   value: "3",
        //   label: "已完成",
        // },
      ];
      return this.tabName === "audit" ? auditOptions : cooperateOptions;
    },
  },
  methods: {
    indexMethod(index) {
      return (
        (this.queryParams.pageNum - 1) * this.queryParams.pageSize + index + 1
      );
    },
    async onSearch() {
      try {
        this.loading = true;
        const fetchMethod =
          this.tabName === "audit" ? getAuditList : getCooperateList;
        const res = await fetchMethod({
          ...this.queryParams,
          ...this.formInline,
        });
        this.tableData = res.data.list;
        this.total = res.data.count;
      } catch (error) {
        console.debug(error);
      } finally {
        this.loading = false;
      }
    },
    handleReset() {
      this.formInline = {};
      this.onSearch();
    },
    handleSearch() {
      this.queryParams.pageNum = 1;
      this.onSearch();
    },
    handleTabChange(tab) {
      this.tabName = tab.name;
      this.handleReset();
    },
    handleSizeChange(val) {
      if (this.queryParams.pageSize * val > this.total) {
        this.queryParams.pageNum = 1;
      }
      this.onSearch();
    },
    handlepageNumChange() {
      this.onSearch();
    },
    handleEditContext(row) {
      const params = {
        docNo: row.docNo,
        editContentType: "document"
      }
      if (this.tabName === "cooperate") {
        params.userNo = this.$store.state.user.userNo
        params.collaborate = true
        params.validToken = ''
      } else {
        params.audit = row.id
      }
      const query = qs.stringify(params);
      window.open(`/editor/view?` + query, "_blank");
    },
  },
  mounted() {
    this.handleSearch();
  },
};
</script>
